package edu.ucla.cs.typecast.net;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import edu.ucla.cs.typecast.bloomfilter.BloomFilter;

/**
 * This class notifies the TypeCast routing layer of the LBF associated with a 
 * multicast group. 
 * 
 * @date Jul 25, 2007 
 */

public class LBFNotifier {
    public void updateLBF(EndPoint endPoint, BloomFilter filter) throws IOException { 
        try { 
        	if (endPoint.getAddress().isMulticastAddress()) { 
	            byte[] buf = new byte[2048]; 
	            DatagramSocket socket = new DatagramSocket(); 
	            int index = 0; 
	            buf[index++] = 1; // number of groups;
	            
	            System.arraycopy(endPoint.getAddress().getAddress(), 0, buf, index, 4);             
	            index += 4; 
	            System.arraycopy(filter.getValue(), 0, buf, index, BloomFilter.SIZE);             
	            index += BloomFilter.SIZE; 
	            DatagramPacket packet = new DatagramPacket(buf, 0, index, InetAddress.getByName("127.0.0.1"), TypeCastConstants.LBF_PORT); 
	            socket.send(packet); 
        	}
        } catch(Throwable t) { 
            t.printStackTrace(); 
        }
    }
}
